{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 08\n",
    "# 2 × 2矩阵的行列式值\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "def determinant_2x2(matrix):\n",
    "    if len(matrix) != 2 or len(matrix[0]) != 2:\n",
    "        raise ValueError(\"Matrix must be 2x2\")\n",
    "    a = matrix[0][0]\n",
    "    b = matrix[0][1]\n",
    "    c = matrix[1][0]\n",
    "    d = matrix[1][1]\n",
    "    det = a * d - b * c\n",
    "    return det"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:20:54.269476Z",
     "end_time": "2024-07-03T16:20:54.274509Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵行列式: 10\n"
     ]
    }
   ],
   "source": [
    "# 示例用法\n",
    "A = [[3, 2],\n",
    "     [1, 4]]\n",
    "det = determinant_2x2(A)\n",
    "print(\"矩阵行列式:\", det)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:21:06.130836Z",
     "end_time": "2024-07-03T16:21:06.139374Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
